<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>
    <meta name="viewport"
          content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <title>聊天室移动版</title>
    <script type="text/javascript" src="js/pageLoadBefore.js"></script>
    <link rel="stylesheet" href="css/style.css"/>
    <link rel="stylesheet" href="css/jquery.emojiarea.css"/>
</head>
<body class="ub ub-ver">
<div class="ub top uc-t">
    <div class="title uc-t">公告</div>
    <div class="ub-f"></div>
    <div class="titleR uc-t" onclick="people(event);">在线：268人</div>
</div>
<div class="ub-f ub center">
    <div class="ub-f container scrollbar">
        <div class="ub temLine">
            <div class="userPicCon"><img src="img/PicCustomer1.png"></div>
            <div class="ub-f">
                <div class="">张三 皇冠 19:30</div>
                <div class="charText">
                    <div class="arrowLeft"></div>
                    考虑到解放和斯科拉电话费奥斯卡了电话费
                </div>
            </div>
        </div>
    </div>

    <div class="people scrollbar"></div>

</div>
<form>
    <div class="ub footer">
        <div class="emoSend"><span class="emotion"></span></div>
        <div class="ub-f">
            <textarea id="input" rows="5" class="emojis-wysiwyg"></textarea>
        </div>
        <div class="submit ub ub-ac ub-pc">发送</div>
    </div>
</form>

<script type="text/javascript" src="js/jquery-3.1.1.min.js"></script>
<script type="text/javascript" src="js/jquery.emojiarea.js"></script>
<script type="text/javascript" src="js/emojis.js"></script>
<script type="text/javascript">

    (function () {//初始化输入框，绑定表情
        $('.emojis-wysiwyg').emojiarea({wysiwyg: true});
    })();

    var $input = $('#input'), $inputDiv = $('.emoji-wysiwyg-editor'), $container = $('.container'), $submit = $('.submit'), $p = $(".people");

    $container.scrollTop($container[0].scrollHeight);//控制聊天区域滚动条到底部

    $submit.on('click', function () {//发送消息
        sendMsg();
    });

    function addMsg(str) {//向聊天区域添加消息
        var myDate = new Date(),
                h = myDate.getHours(),
                m = myDate.getMinutes();
        h = h < 10 ? '0' + h : h;
        m = m < 10 ? '0' + m : m;
        var time = h + ":" + m;
        var msg = '<div class="ub temLine">' +
                '<div class="userPicCon"><img src="img/PicCustomer1.png"></div>' +
                '<div class="ub-f">' +
                '<div class="">张三 皇冠 ' + time + '</div>' +
                '<div class="charText">' +
                '<div class="arrowLeft"></div>' +
                str +
                '</div>' +
                '</div>' +
                '</div>';
        $container.append(msg);
        $container.scrollTop($container[0].scrollHeight);//控制聊天区域滚动条到底部
    }

    function sendMsg() {//发送消息
        var str = $input.val(), strHtml = $inputDiv.html();
        if (str) {
            //todo:转义 str转义， 而不是strHtml

            addMsg(strToHtmlEmoji(str));
            $input.val('');//清空输入框
            $inputDiv.html('');//清空输入框
        }
    }

    $inputDiv.on('keydown', function (event) {//绑定按键
        var e = event || window.event || arguments.callee.caller.arguments[0],
                keyCode = e.keyCode;
        if (keyCode == '13' && e.ctrlKey) {//ctrl+Enter发送
            e.preventDefault();
            sendMsg();
        }
    });

    function people(event) {// 展示/隐藏 在线列表
        if ($p.hasClass('active')) {
            $p.removeClass('active');
        } else {
            $p.addClass('active');
        }

        $container.on('click', function () {
            $p.removeClass('active');
        });

        $p.on('click', function (event) {
            event.stopPropagation();
        });
    }

    (function personLi() {//填充在线列表
        var personHtml = '';
        for (var i = 0; i < 268; i++) {
            personHtml += '<div class="person">' + i + '游客' + Math.floor(Math.random() * 1000) + '</div>';
        }
        $p.html(personHtml);
    })();

</script>
</body>
</html>
